package com.ytt.core.service.service.schoolClass;

import com.ytt.core.model.common.PageDataDTO;
import com.ytt.core.model.schoolClass.SchoolClassDto;
import com.ytt.core.model.schoolClass.SchoolClassEditDto;
import com.ytt.core.service.entity.schoolClass.SchoolClass;
import com.ytt.core.service.exception.NoSuchDataException;
import com.ytt.core.service.mapper.schoolClass.SchoolClassMapper;
import com.ytt.core.service.repository.SchoolClassRepository;
import com.ytt.core.service.service.ProposalQueryParam;
import com.ytt.core.service.utils.BaseUtil;
import com.ytt.core.service.utils.PageDataUtil;
import com.ytt.core.util.jpa.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@Service("schoolClassService")
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class SchoolClassImpl implements  SchoolClassService {
    private  final SchoolClassRepository schoolClassRepository;
    private  final SchoolClassMapper schoolClassMapper;

    @Autowired
    public SchoolClassImpl(SchoolClassRepository schoolClassRepository, SchoolClassMapper schoolClassMapper) {
        this.schoolClassRepository = schoolClassRepository;
        this.schoolClassMapper = schoolClassMapper;
    }

    @Override
    public SchoolClassDto findById(String id) {
        SchoolClass notification = this.schoolClassRepository.findById(Long.valueOf(id)).orElseThrow(() -> new NoSuchDataException(id));
        return this.schoolClassMapper.toDTO(notification);
    }

    @Override
    public List<SchoolClassDto> findAll(String sort, ProposalQueryParam param) {
        List<Sort.Order> orders = PageHelper.toSortOrderList(sort);
        Sort sort1 = Sort.by(orders);
        List<SchoolClass> notifications = this.schoolClassRepository.findAll(
                BaseUtil.<SchoolClass>getBuilder(param).build(), sort1);
        return notifications.stream().map(this.schoolClassMapper::toDTO).collect(Collectors.toList());

    }

    @Override
    public PageDataDTO<SchoolClassDto> findOnePage(Integer page, Integer size, String sort, ProposalQueryParam param) {
        Page<SchoolClass> notificationPage = this.schoolClassRepository.findAll(
                BaseUtil.<SchoolClass>getBuilder(param).build(), PageHelper.generatePageRequest(page, size, sort));
        List<SchoolClassDto> notificationDTOList = notificationPage.getContent().stream().map(this.schoolClassMapper::toDTO)
                .collect(Collectors.toList());
        return PageDataUtil.toPageData(notificationPage, notificationDTOList);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public SchoolClassDto create(SchoolClassEditDto saveDTO) {
        SchoolClass entity = new SchoolClass();
        this.schoolClassMapper.updateEntity(saveDTO, entity);
        entity = this.schoolClassRepository.save(entity);
        return this.schoolClassMapper.toDTO(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public SchoolClassDto update(String id, SchoolClassEditDto saveDTO) {
        Optional<SchoolClass> optional = this.schoolClassRepository.findById(Long.valueOf(id));
        SchoolClass entity = optional.orElseThrow(() -> new NoSuchDataException(id));

        this.schoolClassMapper.updateEntity(saveDTO, entity);
        entity = this.schoolClassRepository.saveAndFlush(entity);
        return this.schoolClassMapper.toDTO(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        SchoolClass entity = this.schoolClassRepository.findById(Long.valueOf(id)).orElseThrow(() -> new NoSuchDataException(id));
        this.schoolClassRepository.delete(entity);
    }
}
